#!/bin/bash

# 六足机器人监控系统启动脚本（支持虚拟环境）

echo "启动六足机器人监控系统..."

# 设置项目目录
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$PROJECT_DIR/venv"
BACKEND_DIR="$PROJECT_DIR/backend"

# 检查是否已安装 python3-full
if ! dpkg -l | grep -q python3-full; then
    echo "警告: 建议安装 python3-full 包"
    echo "运行: sudo apt install python3-full"
fi

# 创建虚拟环境（如果不存在）
if [ ! -d "$VENV_DIR" ]; then
    echo "创建 Python 虚拟环境..."
    python3 -m venv "$VENV_DIR"
    if [ $? -ne 0 ]; then
        echo "错误: 无法创建虚拟环境"
        echo "请确保已安装 python3-venv:"
        echo "sudo apt install python3-venv"
        exit 1
    fi
fi

# 激活虚拟环境
echo "激活虚拟环境..."
source "$VENV_DIR/bin/activate"

# 安装依赖
echo "检查并安装 Python 依赖..."
if [ -f "$BACKEND_DIR/requirements.txt" ]; then
    pip install -r "$BACKEND_DIR/requirements.txt"
else
    echo "安装基础依赖..."
    pip install fastapi uvicorn paho-mqtt websockets
fi

# 启动后端服务
echo "启动后端服务..."
echo "服务将在 http://0.0.0.0:8000 上运行"
echo "WebSocket 端点: ws://0.0.0.0:8000/ws"
echo ""
echo "按 Ctrl+C 停止服务"

cd "$BACKEND_DIR"
uvicorn main:app --host 0.0.0.0 --port 8000 --reload

